package com.vhly.ddxdecompiler.dexreader.file.dalv.code;

import com.vhly.ddxdecompiler.dexreader.io.DexInputStream;

import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-10-21
 * Time: 13:49:42
 *
 * @author vhly[FR]
 */
public class SwitchData extends DalvInsn {

    private boolean packed;
    private Vector<Integer> cases;
    private Vector<Integer> targets;

    public SwitchData(boolean bp) {
        super();
        packed = bp;
        cases = new Vector<Integer>();
        targets = new Vector<Integer>();
    }

    public int read(DexInputStream din) throws IOException {
        int ret = 0;
        if (packed) {
            ret = readPackedSwitch(din);
        } else {
            ret = readSwitch(din);
        }
        return ret;
    }

    private int readSwitch(DexInputStream din) throws IOException {
        int count = 0;
        int outsz = din.readUnsignedShort();
        count++;
        int c;
        for (int i = 0; i < outsz; i++) {
            c = din.readInt();
            count += 2;
            cases.add(c);
        }
        for (int i = 0; i < outsz; i++) {
            c = din.readInt();
            count += 2;
            targets.add(c);
        }
        return count;
    }

    private int readPackedSwitch(DexInputStream din) throws IOException {
        int count = 0;
        int outsz = din.readUnsignedShort();
        int firstCase = din.readInt();
        count += 3;
        // 类似于 tableswitch 每一个case 递增
        int t;
        for (int i = 0; i < outsz; i++) {
            cases.add(firstCase + i);
            t = din.readInt();
            targets.add(t);
            count += 2;
        }
        return count;
    }

    /**
     * 转换为Java ByteCode
     *
     * @return byte[]
     */
    public byte[] reverseJava() {
        return new byte[0];
    }
}
